package net.lazyandroid.namewars.utils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

// This is a convenience class for handling the storage of preferences
public class PreferenceStore 
{
	Activity mNameWars ; 

    PreferenceStore(Activity namewars) 
    {
    	mNameWars = namewars;
    }

    public static long DAY_MS = 86400 * 1000;

    public boolean needToUpdate(String category, long period) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mNameWars);
        long now = System.currentTimeMillis();
        String key = "lastupdate_" + category;
        long lastTime = prefs.getLong(key, -1);
        if (now - lastTime > period) 
        {
            System.out.println("Need-to-update: " + category);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putLong(key, now);
            editor.commit();
            return true;
        } 
        else 
        {
            return false;
        }
    }

    public void putString(String prop, String value) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mNameWars);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(prop, value);
        editor.commit();
    }
    public String getString(String prop, String defValue) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mNameWars);
        return prefs.getString(prop, defValue);
    }
    public void putBoolean(String prop, boolean value) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mNameWars);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(prop, value);
        editor.commit();
    }
    public boolean getBoolean(String prop, boolean defValue) 
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mNameWars);
        return prefs.getBoolean(prop, defValue);
    }
}